package com.integralPerformance.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.integralPerformance.common.Result;
import com.integralPerformance.listener.ServiceListener;
import com.integralPerformance.pojo.excel.ServiceInfoExcel;
import com.integralPerformance.pojo.form.add.AddServiceInfo;
import com.integralPerformance.pojo.form.update.UpdateServiceInfo;
import com.integralPerformance.pojo.query.GetMyServiceInfoQuery;
import com.integralPerformance.pojo.query.ServiceInfoQuery;
import com.integralPerformance.pojo.vo.BaseVo;
import com.integralPerformance.pojo.vo.ExportServiceInfoVo;
import com.integralPerformance.pojo.vo.ServiceInfoByNameAndYearVo;
import com.integralPerformance.pojo.vo.ServiceInfoVo;
import com.integralPerformance.service.impl.ServiceInfoServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/s2/service")
@Tag(name = "教师公共服务积分的导入导出增删查改", description = "教师公共服务积分的导入导出增删查改")
public class ServiceController {


    /**
     * curd 维护教师公共服务积分接口
     */
    @Autowired
    ServiceInfoServiceImpl serviceInfoService;

    @Operation(summary = "查询获取", description = "查询获取")
    @PostMapping("/getServiceInfo")
    public Result<BaseVo<ServiceInfoVo>> getServiceInfo(@RequestBody ServiceInfoQuery serviceInfoQuery) {
        return serviceInfoService.getServiceInfo(serviceInfoQuery);
    }

    @Operation(summary = "查询获取tree", description = "查询获取tree")
    @PostMapping("/getServiceInfoByTree")
    public Result<BaseVo<ServiceInfoByNameAndYearVo>> getServiceInfoByTree(@RequestBody ServiceInfoQuery serviceInfoQuery) {
        return serviceInfoService.getServiceInfoByTree(serviceInfoQuery);
    }


    // 添加公共服务积分
    @Operation(summary = "添加一个公共服务积分", description = "添加一个公共服务积分")
    @PostMapping("/addServiceInfo")
    public Result<String> addServiceInfo(@RequestBody AddServiceInfo addServiceInfo) {
        return serviceInfoService.addServiceInfo(addServiceInfo);
    }

    // 更改公共服务积分
    @Operation(summary = "更改公共服务积分", description = "更改公共服务积分")
    @PostMapping("/updateServiceInfo")
    public Result<String> updateServiceInfo(@RequestBody UpdateServiceInfo updateServiceInfo) {
        return serviceInfoService.updateServiceInfo(updateServiceInfo);
    }

    // 修改公共服务积分 （批量）删除公共服务积分
    @Operation(summary = "删除一个公共服务积分", description = "删除一个公共服务积分")
    @DeleteMapping("/deleteServiceInfoById/{id}")
    public Result<String> deleteServiceInfoById(@PathVariable String id) {
        return serviceInfoService.deleteServiceInfoById(id);
    }

    @Operation(summary = "删除多个公共服务积分", description = "删除多个公共服务积分")
    @DeleteMapping("/deleteServiceInfoByIds")
    public Result<String> deleteServiceInfoByIds(@RequestParam("ids") @Parameter(description = "选择删除的公共服务积分标准列表") List<Long> ids) {
        return serviceInfoService.deleteServiceInfoByIds(ids);
    }

    //教师个人获取自身公共服务积分信息。
    @Operation(summary = "教师个人获取自身公共服务积分信息", description = "教师个人获取自身公共服务积分信息")
    @PostMapping("/getServiceInfoById")
    public Result<BaseVo<ServiceInfoVo>> getServiceInfoById(@RequestBody GetMyServiceInfoQuery getMyServiceInfoQuery) {
        return serviceInfoService.getServiceInfoById(getMyServiceInfoQuery);
    }

    @Operation(summary = "通过工号获取自身公共服务积分信息", description = "教师个人获取自身公共服务积分信息无分页版本，用于feign自动计算教师积分")
    @PostMapping("/getServiceInfoByTeacherId")
    public Result<List<ServiceInfoVo>> getServiceInfoByTeacherId(@RequestParam("teacherId") String teacherId) {
        return serviceInfoService.getServiceInfoByTeacherId(teacherId);
    }

    //公共服务积分积分导入
    @Operation(summary = "Excel批量导入公共服务积分信息", description = "Excel批量导入公共服务积分信息")
    @PostMapping("/addByBatch")
    @Transactional
    public Result<Map<String, Object>> addBatch(@RequestParam("file") @Parameter(description = "添加Excel文件") MultipartFile file) throws IOException {

        ServiceListener serviceListener = new ServiceListener(serviceInfoService);
        EasyExcel.read(file.getInputStream(), ServiceInfoExcel.class, serviceListener).sheet().doRead();
        List<ServiceInfoVo> saveDataIdList = serviceListener.getSaveDataIdList();
        List<ServiceInfoExcel> sameDataList = serviceListener.getSameDataList();
        Map<String, Object> map = new HashMap<>();
        map.put("invalidData", sameDataList);
        map.put("saveData", saveDataIdList);

        return Result.success(map);
    }

    //公共服务积分积分导出
    @Operation(summary = "Excel批量导出公共服务积分信息", description = "Excel批量导出公共服务积分信息")
    @PostMapping("/exportByBatch")
    @Transactional
    public void exportByBatch(@RequestBody ServiceInfoQuery serviceInfoQuery, HttpServletResponse response) throws IOException {

        List<ExportServiceInfoVo> list = serviceInfoService.downloadByBatch(serviceInfoQuery);

        EasyExcel.write(response.getOutputStream())
                .head(ExportServiceInfoVo.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("公共服务积分模板")
                .doWrite(list);
    }

}
